    <?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NodeDao
 *
 * @author rodrigo
 */
class NodeDao {
    
        public $taxoRoot;
        public $root;
        
        public $taxoAnimalia;
        public $reinoAnimalia;
        
        public $taxoPlantae;
        public $reinoPlantae;

        public $taxoCordata;
        public $filoCordata;

        public $taxoPorifero;
        public $filoPorifero;

        public function  __construct() {

        if(Config::$admRequest){
             require_once '../dinamic/taxo.php';
             require_once '../dinamic/node.php';
        }else{
             require_once 'dinamic/taxo.php';
             require_once 'dinamic/node.php';
        }
      
        $this->taxoRoot =  new taxo(null, null, null, null,null);
        $this->taxoRoot->taxo =  'Reinos';

        $this->root = new node(1,null, $this->taxoRoot);

        $this->taxoAnimalia = new taxo(null, null, null, null,null);

        $this->taxoAnimalia->taxo = 'Reino';
        $this->taxoAnimalia->nome = 'Animalia';
        $this->taxoAnimalia->descricao = 'O reino animalia é composto pelos mamíferos no qual o homem está incluido ...';

        $this->reinoAnimalia = new node(2,$this->root, $this->taxoAnimalia);


        $this->taxoPlantae = new taxo(null, null, null, null,null);

        $this->taxoPlantae->taxo = 'Reino';
        $this->taxoPlantae->nome = 'Plantae';
        $this->taxoPlantae->descricao = 'O reino plantae é composto pelos seres autotrófos...';

        $this->reinoPlantae = new node(3,$this->root, $this->taxoPlantae);

        $this->taxoCordata =  new taxo(null, null, null, null,null);

        $this->taxoCordata->taxo = 'Filo';
        $this->taxoCordata->nome = 'Cordata';
        $this->taxoCordata->descricao= 'São os animais que tem uma estrutura cervical de sustentação...';

        $this->filoCordata = new node(4, $this->reinoAnimalia, $this->taxoCordata);

        $this->taxoPorifero =  new taxo(null, null, null, null,null);

        $this->taxoPorifero->taxo = 'Filo';
        $this->taxoPorifero->nome = 'Porifero';
        $this->taxoPorifero->descricao= 'São animais muito primitivos, conhecidos por esponjas, que vivem em água salgada ou doce.';

        $this->filoPorifero = new node(5, $this->reinoAnimalia, $this->taxoPorifero);

         $this->root->append($this->reinoAnimalia);
         $this->root->append($this->reinoPlantae);

         $this->reinoAnimalia->append($this->filoCordata);
         $this->reinoAnimalia->append($this->filoPorifero);
    }

    public function getNodeById($id){
           if($id==1){
                return $this->root;
            }if($id==2){
                return $this->reinoAnimalia;
            }if($id==3){
                return $this->reinoPlantae;
            }if($id==4){
                return $this->filoCordata;
            }if($id==5){
                return $this->filoPorifero;
            }

    }
    
    static $node;
    static $taxo;
    static $son;

    static function initNode(){
        if(Config::$admRequest){
             require_once '../dinamic/taxo.php';
             require_once '../dinamic/node.php';
        }else{
             require_once 'dinamic/taxo.php';
             require_once 'dinamic/node.php';
        }

        NodeDao::$node = 0;
        NodeDao::$taxo = new taxo('', '','','');

    }

    public static function db_node_by_id($dbc, $id)
  {
    $query = 'SELECT * FROM taxo WHERE id='.$id.' LIMIT 1';
    return NodeDao::db_select($dbc, $query);
  }

  public static function insert($dbc)
  {
    $query = 'INSERT INTO taxo VALUES (NULL,\''.NodeDao::$taxo->taxo.'\',\''.NodeDao::$taxo->nome.'\',\''.NodeDao::$taxo->descricao.'\',\''.NodeDao::$taxo->data.'\','.  NodeDao::$node.')';
    $result = mysqli_query($dbc, $query);
  }

  public static function update($dbc)
  {
    $query = 'UPDATE taxo SET taxo=\''.NodeDao::$taxo->taxo.'\', nome=\''.NodeDao::$taxo->nome.'\', descricao=\''.NodeDao::$taxo->descricao.'\', data=\''.NodeDao::$taxo->data.'\' WHERE id='.NodeDao::$node;
    //echo $query;
    $result = mysqli_query($dbc, $query);
  }

  public static function delete($dbc,$id){
      
      $current = NodeDao::db_node_by_id($dbc, $id);
      echo $current->taxo->taxo.' '.$current->taxo->nome.'</br>';
      $iterator = $current->getIterator();

      while($iterator->valid()){
          NodeDao::delete($dbc, $iterator->current()->id);
          $iterator->next();
      }

      $query = 'DELETE FROM taxo WHERE id = '.$current->id;
      $result = mysqli_query($dbc, $query);
  }
  static $flag;
  public static function getSonNumber($dbc,$id){
      $current = NodeDao::db_node_by_id($dbc, $id);

      $query = 'SELECT * FROM especie WHERE idtaxo = '.$id;
      $result = mysqli_query($dbc, $query);
      //if(NodeDao::$flag){
      NodeDao::$son+= mysqli_num_rows($result);
      //}
      $iterator = $current->getIterator();

      while($iterator->valid()){
          //NodeDao::$flag=true;
          NodeDao::getSonNumber($dbc, $iterator->current()->id);
          $iterator->next();
      }
      
  }

    protected static function db_select($dbc, $query)
    {
        if ($result = mysqli_query($dbc, $query)) {
            if (mysqli_num_rows($result) > 0) {
                    $row = mysqli_fetch_array($result, MYSQLI_NUM);               
                    $id= $row[0];
                    $taxo = $row[1];
                    $nome = $row[2];
                    $descricao = $row[3];
                    $data = $row[4];
                    $id_pai = $row[5];

                    $q =  'SELECT * FROM taxo WHERE id='.$id_pai.' LIMIT 1';

                    
                    $pai = new node(null, null, null);
                     if ($result = mysqli_query($dbc, $q)) {
                        if (mysqli_num_rows($result) > 0) {
                                $row = mysqli_fetch_array($result, MYSQLI_NUM);
                                $p_id= $row[0];
                                $p_taxo = $row[1];
                                $p_nome = $row[2];
                                $p_descricao = $row[3];
                                $p_especie = $row[4];
                                $p_data = $row[5];
                                

                               $taxoPai = new taxo($p_taxo, $p_nome, $p_descricao, $p_data);
                               $pai = new node($p_id, null, $taxoPai);
                            }
                     }
                     
                     $q =  'SELECT * FROM taxo WHERE idpai='.$id_pai;

                       if ($result = mysqli_query($dbc, $q)) {
                             if (mysqli_num_rows($result) > 0) {
                                  while($row = mysqli_fetch_array($result, MYSQLI_NUM)){                                           
                                            $i_id= $row[0];
                                            $i_taxo = $row[1];
                                            $i_nome = $row[2];
                                            $i_descricao = $row[3];
                                            $i_data = $row[4];
                                            $i_id_pai = $row[5];

                                            $i_temp= new taxo($i_taxo, $i_nome, $i_descricao, $i_data);
                                            $i_nodeTemp = new node($i_id, $pai, $i_temp);
                                            $pai->append($i_nodeTemp);
                                  }
                             }
                       }

                       $temp= new taxo($taxo, $nome, $descricao, $data);
                       $nodeTemp = new node($id, $pai, $temp);

                       $q =  'SELECT * FROM taxo WHERE idpai='.$id;
                       if ($result = mysqli_query($dbc, $q)) {
                             if (mysqli_num_rows($result) > 0) {
                                 while( $row = mysqli_fetch_array($result, MYSQLI_NUM) ){
                                            $f_id= $row[0];
                                            $f_taxo = $row[1];
                                            $f_nome = $row[2];
                                            $f_descricao = $row[3];
                                            $f_data = $row[4];
                                            $f_id_pai = $row[5];

                                            $f_temp= new taxo($f_taxo, $f_nome, $f_descricao, $f_data);
                                            $f_nodeTemp = new node($f_id, $nodeTemp, $f_temp);
                                            $nodeTemp->append($f_nodeTemp);

                                  }
                             }
                       }

                      return $nodeTemp;
           
                }
            }
      }
}
?>
